package com.itrui.sockets;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class TcpSendFileClient {
    public static int port = 5555;
    public static String host = "192.168.92.1";
    public static Socket socket = null;

    @SuppressWarnings("resource")
    public static void main(String[] args) throws UnknownHostException {
        //创建socket连接
        createSocket();
        String s = InetAddress.getLocalHost().toString();

        try {
            //发送文件的发放，需要文件价路径
            sendMultipleFile("D:\\RuiProject\\FileTest");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭socket连接
            closeSocket();
        }
    }

    public static void sendMultipleFile(String path) throws Exception {
        String[] fileNames = getFileName(path);
        sendSingleFile(path, fileNames);
    }

    public static String[] getFileName(String path) {
        File file = new File(path);
        return file.list();
    }

    public static File[] createFile(String path, String[] pathnames) {
        File[] files = new File[pathnames.length];
        try {
            for (int i = 0; i < pathnames.length; i++) {
                File file = new File(path + "\\" + pathnames[i]);
                if (file.exists() && file.isFile()) {
                    file.createNewFile();
                    files[i] = file;
                }
            }
            return files;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return files;
    }

    public static void sendSingleFile(String path, String[] pathNames) throws Exception {
        File[] files = createFile(path, pathNames); //文件夹下的文件

        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        dos.writeInt(files.length); //文件个数
        dos.flush();

        //将文件名和文件长度先发送过去
        for (int i = 0; i < files.length; i++) {
            System.out.println(files.length + "==" + files[i].getName() + "==" + files[i].length());

            dos.writeUTF(files[i].getName()); //文件名
            dos.flush();

            dos.writeLong(files[i].length()); //文件长度
            dos.flush();
        }

        System.out.println("开始发送文件...");
        byte[] bytes = new byte[1024];
        int length = 0;

        //发送文件内容
        for (int i = 0; i < files.length; i++) {
            FileInputStream fis = new FileInputStream(files[i]);

            while ((length = fis.read(bytes, 0, bytes.length)) != -1) {
                dos.write(bytes, 0, length);
                dos.flush();
            }

            System.out.println("已经发送完毕第" + i + "个文件");
        }

        System.out.println("文件发送完毕...");
    }

    public static void createSocket() {
        try {
            socket = new Socket(host, port);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void closeSocket() {
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
